var app = getApp()
var cartUtil = require("../../utils/cartUtil.js")
var orderModel = require("../../model/orderModel.js")
var quanModel = require("../../model/quanModel.js")
var util = require("../../utils/util.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user: null,
    goodsList: [],
    totalPrice: 0,
    type: 0,
    tuanId: 0,
    usables: [],
    disables: [],
    showQuans: false,
    reqPrice: 0,
    salePrice: 0,
    quanTags: ['可用优惠券', '不可用优惠券'],
    quanTagIndex: 0,
    quan: {
      name: '',
      cqId: 0
    },
    quanMoney: 0,
    quanIndex: -1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.init()
    var type = options.type
    if (type == 1) { //创建团
      this.setData({
        type: type,
        tuanId: options.tuanId
      })
    } else if (type == 2) { //加入团
      this.setData({
        type: type,
        tuanId: options.tuanId,
        trId: options.tuanRecordId
      })
    } else { //非拼团,加载优惠券
      this.initQuan()
    }
    var cusId = options.cusId
    if (cusId) { //通过分享邀请卡进入
      this.setData({
        cusId: cusId
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  init() {
    var list = app.data.selectGoods
    //计算总价
    var price = 0
    for (var i = 0; i < list.length; i++) {
      price += list[i].price * list[i].num
    }
    this.setData({
      goodsList: list,
      totalPrice: price,
      reqPrice: price,
      user: app.data.user
    })
  },
  initQuan() {
    util.showLoading("加载中")
    var that = this
    quanModel.getAllQuan(res => {
      wx.hideLoading()
      var quans = res.data
      var list = that.data.goodsList
      var usables = []
      var disables = []
      for (var i = 0; i < quans.length; i++) {
        var quan = quans[i]
        if (quan.limitProduct == 0) {
          if (that.data.totalPrice >= quan.limitMoney) {
            usables.push(quan)
            continue
          }
        } else {
          var totalPrice = 0
          for (var j = 0; j < list.length; j++) {
            if (list[j].type == quan.limitProduct) totalPrice += list[j].price * list[j].num
          }
          if (totalPrice >= quan.limitMoney) {
            usables.push(quan)
            continue
          }
        }
        disables.push(quan)
      }
      var tags = ['可用优惠券(' + usables.length + ")", '不可用优惠券(' + disables.length + ")"]
      that.setData({
        usables: usables,
        disables: disables,
        quanTags: tags
      })
      // console.log("usable",usables)
      // console.log("disable",disables)                                           
    })
  },
  // 空方法
  space() {

  },
  //优惠券标签
  switchQuanTag(e) {
    this.setData({
      quanTagIndex: e.currentTarget.id
    })
  },
  //选择优惠券
  bindSelectQuan(e) {
    var index = e.currentTarget.id
    var quans = this.data.usables
    var quan = quans[index]
    //计算有优惠后价格
    var sumPrice = this.data.totalPrice
    if (quan.limitProduct != 0) {
      sumPrice = 0
      var list = this.data.goodsList
      for (var j = 0; j < list.length; j++) {
        if (list[j].type == quan.limitProduct) sumPrice += list[j].price * list[j].num
      }
    }
    var quanMoney = 0
    if (quan.type == 1) {
      quanMoney = quan.money
    } else {
      quanMoney = sumPrice - sumPrice * quan.money / 10
    }

    var reqPrice
    if (quanMoney > this.data.totalPrice) {
      reqPrice = this.data.totalPrice
      quanMoney = reqPrice
    } else reqPrice = this.data.totalPrice - quanMoney
    this.setData({
      quan: quan,
      quanIndex: index,
      reqPrice: reqPrice.toFixed(2),
      salePrice: quanMoney.toFixed(2),
      quanMoney: quanMoney.toFixed(2),
      showQuans: false,
    })
  },
  //优惠券弹窗
  bindQuanModal() {
    this.setData({
      showQuans: !this.data.showQuans
    })
  },
  //发送支付
  sendPay() {
    var type = this.data.type
    var data = {
      totalPrice: this.data.totalPrice,
      type: type,
      tuanId: this.data.tuanId,
      tuanRecordId: this.data.trId || 0,
      shareCusId: this.data.cusId || 0,
      reqPrice: this.data.reqPrice,
      salePrice: this.data.salePrice,
      quanId: this.data.quan.cqId,
      quanName: this.data.quan.name,
      quanMoney: this.data.quanMoney,
    }
    if (type == 1) {
      this.createTuanOrder(data)
    } else if (type == 2) {
      this.joinTuanOrder(data)
    } else {
      this.createGoodsOrder(data)
    }

  },
  //商品订单
  createGoodsOrder(data) {
    orderModel.createGoodsOrder(data, this.data.goodsList, res => {
      if (res.status == 1) {
        util.failToast(res.msg)
        return
      }
      util.successToast("支付成功")
      setTimeout(() => {
        wx.redirectTo({
          url: '../user_order/user_order',
        })
      }, 800)
    })
  },
  //创建团
  createTuanOrder(data) {
    orderModel.createTuanOrder(data, res => {
      if (res.status == 1) {
        util.failToast(res.msg)
        return
      }
      util.successToast("支付成功")
      setTimeout(() => {
        wx.redirectTo({
          url: '../tuan_join/tuan_join?trId=' + res.data.trId,
        })
      }, 800)
    })
  },
  //加入团
  joinTuanOrder(data) {
    orderModel.joinTuanOrder(data, res => {
      if (res.status == 1) {
        util.failToast(res.msg)
        return
      }
      var url
      if (res.data.status == 3) url = "../tuan_join/tuan_join?trId=" + res.data.trId
      else if (res.data.status == 2) url = "../user_order_info/user_order_info?id=" + res.data.orderId
      util.successToast("支付成功")
      setTimeout(() => {
        wx.redirectTo({
          url: url,
        })
      }, 800)
    })
  },
})